import 'package:flutter/material.dart';
import 'package:flutter_smart_heating/common/http.dart';
import 'package:flutter_smart_heating/ui/ui.dart';

class HomeStationInfoView extends StatefulWidget {
  const HomeStationInfoView({super.key});

  @override
  State<HomeStationInfoView> createState() => _HomeStationInfoViewState();
}

class _HomeStationInfoViewState extends State<HomeStationInfoView> {
  Map<String, dynamic> data = {};
  @override
  void initState() {
    fetchInfo();
    super.initState();
  }

  fetchInfo() async {
    var res = await Http.instance.post('/api/basic/index/count');
    if (res.code == 1) {
      setState(() {
        data = res.data;
      });
    }
  }

  bool get isUser => const ValueKey('user') == widget.key;
  @override
  Widget build(BuildContext context) {
    return UiPanel(
      padding: EdgeInsets.symmetric(vertical: isUser ? 0 : 15),
      backgroundColor: const ValueKey('user') == widget.key
          ? Colors.transparent
          : UiTheme(context).match(
              light: const Color.fromARGB(255, 241, 248, 254),
            ),
      border: isUser
          ? Border.all(color: Colors.transparent)
          : UiTheme(context).match(
              light: Border.all(
                width: 0.2,
                color: const Color.fromARGB(255, 174, 190, 225),
              ),
            ),
      child: Row(
        children: [
          itemView("热企", data['company'] ?? 0, 'company'),
          itemLineView(context),
          itemView("热站", data['station'] ?? 0, 'station'),
          itemLineView(context),
          itemView("小区", data['estate'] ?? 0, 'estate'),
          itemLineView(context),
          itemView(
              "室温", data['indoor'] ?? 0, '/information/indoor/temperature'),
        ],
      ),
    );
  }

  Widget itemLineView(context) {
    return Container(
      height: 35,
      padding: const EdgeInsets.only(bottom: 12),
      child: VerticalDivider(
        color: UiTheme(context).match(
          light: UiTheme.primary.withOpacity(0.8),
        ),
      ),
    );
  }

  Widget itemView(name, value, String url) {
    return Expanded(
      child: UiOpen(
        url: url.startsWith('/') ? url : '/basic/$url',
        child: Column(
          children: [
            UiLinearGradientMask(
              gradient: !isUser,
              child: Text(
                "$value",
                style: TextStyle(
                    fontSize: 24,
                    color: isUser ? UiTheme.primary : Colors.white,
                    height: 1.3),
              ),
            ),
            Text(
              "$name",
              style: const TextStyle(fontSize: 14),
            ),
          ],
        ),
      ),
    );
  }
}
